package com.unionfetch.content_home.model

import com.google.gson.Gson
import com.unionfetch.content_home.api.HomeApiManager
import com.unionfetch.content_home.bean.AccountAuditType
import com.unionfetch.content_home.bean.AccountCount
import com.unionfetch.content_home.bean.AccountLeverInfo
import com.unionfetch.content_home.bean.DemoAccountInfo
import com.unionfetch.content_home.bean.LiveAccountInfo
import com.unionfetch.content_home.bean.request.AddDemoAccountRequest
import com.unionfetch.content_home.bean.request.AddLiveAccountRequest
import com.unionfetch.content_home.bean.request.ModifyDemoLeverRequest
import com.unionfetch.content_home.bean.request.ModifyLeverRequest
import com.unionftech.common.common.bean.SystemUserInfo
import com.unionfetch.content_home.contract.AccountContract
import com.unionftech.common.base.mvp.impl.BaseModel
import com.unionftech.common.base.mvp.impl.IBaseCallBack
import com.unionftech.common.bean.ResponseResult
import com.unionftech.common.network.SimpleNetWork
import okhttp3.RequestBody
import okhttp3.RequestBody.Companion.toRequestBody

class AccountModel : BaseModel(), AccountContract.IAccountModel {


    override fun getUserLiveAccountList(
        token: String, callBack: IBaseCallBack<ResponseResult<LiveAccountInfo>>
    ) {
        execute {
            val result = request {
                HomeApiManager.getInstance().apiAccount().getUserLiveAccountList(token)
            }
            callBack.onResponse(result)
        }
    }

    override fun getUserDemoAccountList(
        token: String, callBack: IBaseCallBack<ResponseResult<DemoAccountInfo>>
    ) {
        execute {
            val result = request {
                HomeApiManager.getInstance().apiAccount().getUserDemoAccountList(token)
            }
            callBack.onResponse(result)
        }
    }

    override fun createDemoAccount(
        token: String, request: AddDemoAccountRequest, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val result = request {
                val json = Gson().toJson(request, AddDemoAccountRequest::class.java)
                val body = json.toRequestBody(SimpleNetWork.JSON)
                HomeApiManager.getInstance().apiAccount().createDemoAccount(token, body)
            }
            callBack.onResponse(result)
        }
    }

    override fun createLiveAccount(
        token: String, request: AddLiveAccountRequest, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val result = request {
                val json = Gson().toJson(request, AddLiveAccountRequest::class.java)
                val body = json.toRequestBody(SimpleNetWork.JSON)
                HomeApiManager.getInstance().apiAccount().createLiveAccount(token, body)
            }
            callBack.onResponse(result)
        }
    }

    override fun modifyLeverage(
        token: String, request: ModifyLeverRequest, callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val result = request {
                val json = Gson().toJson(request, ModifyLeverRequest::class.java)
                val body = json.toRequestBody(SimpleNetWork.JSON)
                HomeApiManager.getInstance().apiAccount().modifyLeverage(token, body)
            }
            callBack.onResponse(result)
        }
    }

    override fun modifyDemoLeverage(
        token: String,
        request: ModifyDemoLeverRequest,
        callBack: IBaseCallBack<ResponseResult<Any?>>
    ) {
        execute {
            val result = request {
                val json = Gson().toJson(request, ModifyDemoLeverRequest::class.java)
                val body = json.toRequestBody(SimpleNetWork.JSON)
                HomeApiManager.getInstance().apiAccount().modifyDemoLeverage(token, body)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryLeverage(
        token: String,
        tradeNo: String,
        callBack: IBaseCallBack<ResponseResult<AccountLeverInfo>>
    ) {
        execute {
            val result = request {
                HomeApiManager.getInstance().apiAccount().queryLeverage(token, tradeNo)
            }
            callBack.onResponse(result)
        }
    }

    override fun queryAccountCount(
        token: String,
        userId: String,
        callBack: IBaseCallBack<ResponseResult<AccountCount>>
    ) {
        execute {
            val result = request {
                HomeApiManager.getInstance().apiAccount()
                    .queryAccountCount(
                        token,
                        status = AccountAuditType.AUDITING.status,
                        userId
                    )
            }
            callBack.onResponse(result)
        }
    }
}